<template>
  <!-- 新增部门的弹层 -->
  <el-dialog
    :title="showTitle"
    :visible="showAddDialog"
    :show-close="true"
    @close="closeAddDialog"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="deptForm"
      label-width="120px"
      :model="addDeptsForm"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="addDeptsForm.name"
          style="width:80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="addDeptsForm.code"
          style="width:80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="mannager">
        <el-select
          v-model="addDeptsForm.manager"
          style="width:80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="addDeptsForm.introduce"
          style="width:80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button
          type="primary"
          size="small"
          @click="submitAddDepts"
        >确定</el-button>
        <el-button size="small" @click="closeAddDialog">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDepartments,
  addDepartments,
  getDeptDetail,
  updateDepartments
} from '@/api/departments'
import { getEmployeesSimple } from '@/api/employees'
export default {
  name: '',
  components: {},
  props: {
    showAddDialog: {
      type: Boolean,
      default: false
    },
    node: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.addDeptsForm.id) {
        // 有id就是编辑模式
        // 编辑 张三 => 校验规则 除了我之外 同级部门下 不能有叫张三的
        isRepeat = depts
          .filter(
            item =>
              item.id !== this.addDeptsForm.id &&
              item.pid === this.node.pid
          )
          .some(item => item.name === value)
      } else {
        // 没id就是新增模式
        isRepeat = depts
          .filter(item => item.pid === this.node.id)
          .some(item => item.name === value)
      }
      isRepeat
        ? callback(new Error(`同级部门下已经有${value}的部门了`))
        : callback()
    }
    // 检查编码重复
    const checkCodeRepeat = async(rule, value, callback) => {
      // 先要获取最新的组织架构数据
      //  检查重复规则 需要支持两种 新增模式 / 编辑模式
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.addDeptsForm.id) {
        // 编辑模式  因为编辑模式下 不能算自己
        isRepeat = depts.some(
          item =>
            item.id !== this.addDeptsForm.id &&
            item.code === value &&
            value
        )
      } else {
        // 新增模式
        isRepeat = depts.some(item => item.code === value && value) // 这里加一个 value不为空 因为我们的部门有可能没有code
      }

      isRepeat
        ? callback(new Error(`组织架构中已经有部门使用${value}编码`))
        : callback()
    }
    return {
      addDeptsForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: checkNameRepeat,
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: checkCodeRepeat,
            trigger: 'blur'
          }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            min: 1,
            max: 300,
            message: '部门介绍要求1-50个字符'
          }
        ]
      },
      managerList: []
    }
  },
  computed: {
    showTitle() {
      return this.addDeptsForm.id ? '编辑部门' : '新增子部门'
    }
  },
  watch: {},
  created() {
    this.loadEmployeesList()
    console.log(this.node.id)
  },
  mounted() {},
  methods: {
    closeAddDialog() {
      this.$emit('update:closeAddDialog', false)
    },
    async loadEmployeesList() {
      const data = await getEmployeesSimple(this.addDeptsForm)
      this.managerList = data
    },
    submitAddDepts() {
      this.$refs.deptForm.validate(async valid => {
        if (valid) {
          if (this.addDeptsForm.id) {
            // 编辑模式  调用编辑接口
            await updateDepartments(this.formData)
          } else {
            // 新增模式
            await addDepartments({ ...this.addDeptsForm, pid: this.node.id }) // 调用新增接口 添加父部门的id
          }
          this.$emit('submitAddDepts')
          this.closeAddDialog()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async getDeptsDetail(id) {
      const data = await getDeptDetail(id)
      this.addDeptsForm = data
    }
  }
}
</script>

<style scoped lang="less"></style>
